package com.fita.tencent.entity.webauth;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.wechat.entity.AuthScope;
import com.fita.entity.user.WechatAccount;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "web_auth")
public class WebAuth {
    
    /** uuid - primary key */
	@Id
    private String id;
    
    /** this is from request */
    @JsonIgnore
    private String requestNo;
    
    /** request_no plus client_id as prefix */
    private String state;
    
    /** call back redirect url */
    private String redirectUrl;
    
    /** from which client calls, this should be a host */
    private String client;
    
    /** web auth scope */
    private String scope;
    
    /** openid for the request */
    private String openid;
    
    /** unionid for the request */
    private String unionid;
    
    /** request time */
    @JsonIgnore
    private Date requestTime;
    
    /** visited flag */
    @Column(name = "is_visited")
    private boolean visited;
    
    /** the corresponding wechat account */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIgnore
    private WechatAccount account;
    
    /**
     * The default constructor
     */
    public WebAuth() {
        
    }
    
    /**
     * 
     * @param uuid
     * @param config
     * @return
     */
    public WebAuth init(String uuid, WebAuthConfig config) {
        this.id = uuid;
        this.state = config.getState(this.requestNo);
        this.requestTime = new Date();
        return this;
    }
    
    /**
     * 
     * @param openid
     * @return
     */
    public WebAuth authorize(String openid) {
    	this.openid = openid;
    	this.visited = true;
    	return this;
    }
    
    /**
     * 
     * @param unionId
     * @param wechatAccount
     * @return
     */
    public WebAuth union(String unionId, WechatAccount wechatAccount) {
    	this.unionid = unionId;
    	this.account = wechatAccount;
    	return this;
    }

    /**
     * 
     * @param wechatAccount
     * @return
     */
    public WebAuth account(WechatAccount wechatAccount) {
        this.account = wechatAccount;
        return this;
    }

    /**
     * 
     * @param authScope
     * @return
     */
    public boolean matches(AuthScope authScope) {
        return this.getAuthScope() != null && this.getAuthScope().equals(authScope);
    }
    
    /**
     * 
     * @return
     */
    @JsonIgnore
    public boolean isValid() {
        return this.visited && StringUtils.isNotBlank(this.openid) && StringUtils.isNotBlank(this.redirectUrl);
    }
    
    /**
     * 
     * @return
     */
    @JsonIgnore
    public AuthScope getAuthScope() {
        return AuthScope.scopeOf(this.scope);
    }

    /**
     * @return the id
     */
    public String getId() {
        return this.id;
    }
    
    /**
     * @return the requestNo
     */
    public String getRequestNo() {
        return this.requestNo;
    }

    /**
     * @return the state
     */
    public String getState() {
        return this.state;
    }

    /**
     * @return the redirectUrl
     */
    public String getRedirectUrl() {
        return this.redirectUrl;
    }

    /**
     * @return the client
     */
    public String getClient() {
        return this.client;
    }

    /**
     * @return the scope
     */
    public String getScope() {
        return this.scope;
    }

    /**
     * @return the openid
     */
    public String getOpenid() {
        return this.openid;
    }

    /**
     * @return the unionid
     */
    public String getUnionid() {
        return this.unionid;
    }

    /**
     * @return the requestTime
     */
    public Date getRequestTime() {
        return this.requestTime;
    }

    /**
     * @return the visited
     */
    public boolean isVisited() {
        return this.visited;
    }

    /**
     * @return the account
     */
    public WechatAccount getAccount() {
        return this.account;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
    
    /**
     * @param requestNo the requestNo to set
     */
    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    /**
     * @param state the state to set
     */
    public void setState(String state) {
        this.state = state;
    }

    /**
     * @param redirectUrl the redirectUrl to set
     */
    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    /**
     * @param client the client to set
     */
    public void setClient(String client) {
        this.client = client;
    }

    /**
     * @param scope the scope to set
     */
    public void setScope(String scope) {
        this.scope = scope;
    }

    /**
     * @param openid the openid to set
     */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * @param unionid the unionid to set
     */
    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }

    /**
     * @param requestTime the requestTime to set
     */
    public void setRequestTime(Date requestTime) {
        this.requestTime = requestTime;
    }

    /**
     * @param visited the visited to set
     */
    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    /**
     * @param account the account to set
     */
    public void setAccount(WechatAccount account) {
        this.account = account;
    }
}
